package ynu.edu.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ynu.edu.common.controller.LogicController;
import ynu.edu.utils.Result;
import ynu.edu.utils.TokenUtils;
import ynu.edu.utils.inna.AuthPermission;
import ynu.edu.dao.IBusinessDao;
import ynu.edu.dto.ItemDTO;
import ynu.edu.dto.ShopDTO;
import ynu.edu.entity.Business;
import ynu.edu.entity.Item;
import ynu.edu.entity.Shop;
import ynu.edu.entity.User;
import ynu.edu.service.BusinessService;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@Api(tags = "商家操作控制器")
@RestController
@RequestMapping("/business")
public class BusinessController extends LogicController<BusinessService, IBusinessDao, Business,Long> {
    public BusinessController(@Autowired BusinessService ls) {
        super(ls);
    }
    @Resource
    private ModelMapper modelMapper;

    @AuthPermission(role = User.TYPE_BUSINESS)
    @ApiOperation("商家创建门店")
    @PostMapping("/createShop")
    public Result createShop(@RequestBody Shop shop){
        User user = TokenUtils.getCurrentUser();
        Shop newShop = getService().createShop(user,shop);
        return Result.success(modelMapper.map(newShop, ShopDTO.class));
    }

    @AuthPermission(role = User.TYPE_BUSINESS)
    @ApiOperation("商家创建商品")
    @PostMapping("/createItem")
    public Result createItem(@RequestBody Item item){
        User user = TokenUtils.getCurrentUser();
        Item newItem = getService().createItem(user, item);
        return Result.success(modelMapper.map(newItem, ItemDTO.class));
    }
    @AuthPermission(role = User.TYPE_BUSINESS)
    @ApiOperation("商家获取门店列表")
    @GetMapping("/listMyShops")
    public Result listMyShops(){
        User user = TokenUtils.getCurrentUser();
        List<Shop> myShops = getService().listMyShops(user);
        return Result.success(myShops.stream().map(shop -> modelMapper.map(shop, ShopDTO.class)).collect(Collectors.toList()));
    }

    @AuthPermission(role = User.TYPE_BUSINESS)
    @ApiOperation("商家获取全部商品列表")
    @GetMapping("/listMyItems")
    public Result listMyItems(){
        User user = TokenUtils.getCurrentUser();
        List<Item> myItems = getService().listMyItems(user);
        return  Result.success(myItems.stream().map(item -> modelMapper.map(item, ItemDTO.class)).collect(Collectors.toList()));
    }

}
